package de.fsujena.chat_pi.client.userInterface.GUI;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JTree;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
import javax.swing.tree.DefaultTreeModel;

import de.fsujena.chat_pi.client.Client;

import de.fsujena.chat_pi.ressources.Room;
import de.fsujena.chat_pi.ressources.User;
import de.fsujena.chat_pi.ressources.message.SysMessage.CATEGORY;
import de.fsujena.chat_pi.ressources.message.SysMessage.MESSAGE;

import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JList;
import java.awt.Dimension;
import java.awt.Rectangle;


public class MainWindow {

	private JFrame frmClient;
	private JTextField messageTextField;
	private Client clientObject;
	private StyledDocument doc;
	public DefaultTreeModel treeModel ;
	public JTree userRoomTree;
	User nullUser=null;
	public Room foyer=new Room("Foyer",null,0,nullUser);
	public JMenu mnRoom;
	public JMenu mnUser;
	public JMenuItem mntmMakeRoomPrivate;
	public JMenuItem mntmMakeRoomPublic;
	public JMenu mnWhitelist;
	public JMenu mnBlacklist;
//	/**
//	 * Launch the application.
//	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				
//			}
//		});
//	}
	
	public MainWindow(Client clientObject) {
		this.clientObject = clientObject;

		initialize();

		frmClient.setVisible(true);
		
	}

	/**
	 * @wbp.parser.entryPoint
	 *
	 * Create the application.
	 */
	public MainWindow() {
		try {
			MainWindow window = new MainWindow(clientObject);
			window.frmClient.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		// Main Frame
		frmClient = new JFrame();
		frmClient.setTitle("Client");
		frmClient.setBounds(100, 100, 800, 600);
		frmClient.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Menu
		JMenuBar menuBar = new JMenuBar();
		frmClient.setJMenuBar(menuBar);
		
		JMenu mnClient = new JMenu("Client");
		menuBar.add(mnClient);
		
		JMenuItem mntmConnect = new JMenuItem("Connect");
		mntmConnect.addActionListener(new ClientConnectListener());
		mnClient.add(mntmConnect);
		
		JMenuItem mntmDisconnect = new JMenuItem("Disconnect");
		mntmDisconnect.addActionListener(new ClientDisconnectListener());
		mnClient.add(mntmDisconnect);
		
		JMenuItem mntmRegister = new JMenuItem("Register");
		mntmRegister.addActionListener(new ClientRegisterListener());
		mnClient.add(mntmRegister);
		
		JMenuItem mntmLogin = new JMenuItem("Login");
		mntmLogin.addActionListener(new ClientLoginListener());
		mnClient.add(mntmLogin);
		
		JMenuItem mntmLogout = new JMenuItem("Logout");
		mntmLogout.addActionListener(new ClientLogoutListener());
		mnClient.add(mntmLogout);
		
		JMenuItem mntmSettings = new JMenuItem("Settings");
		mntmSettings.addActionListener(new OpenSettingsListener());
		mnClient.add(mntmSettings);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ClientExitListener());
		
		JMenuItem mntmDeleteHistory = new JMenuItem("Delete History");
		mntmDeleteHistory.addActionListener(new DeleteHistory());
		mnClient.add(mntmDeleteHistory);
		mnClient.add(mntmExit);
		
		
		frmClient.getContentPane().setLayout(new CardLayout(0, 0));
		
		JSplitPane chatMain = new JSplitPane();
		chatMain.setRequestFocusEnabled(false);
		chatMain.setResizeWeight(0.85);
		chatMain.setBounds(new Rectangle(0, 0, 24, 0));
		frmClient.getContentPane().add(chatMain, "name_597957090584681");
		
		JPanel messageBox = new JPanel();
		chatMain.setLeftComponent(messageBox);
		
		JPanel messageSendPanel = new JPanel();
		messageSendPanel.setAlignmentY(Component.TOP_ALIGNMENT);
		messageSendPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		JScrollPane messageViewPanel = new JScrollPane();
		messageViewPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GroupLayout gl_messageBox = new GroupLayout(messageBox);
		gl_messageBox.setHorizontalGroup(
			gl_messageBox.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_messageBox.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_messageBox.createParallelGroup(Alignment.TRAILING)
						.addComponent(messageSendPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
						.addComponent(messageViewPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 673, Short.MAX_VALUE))
					.addGap(17))
		);
		gl_messageBox.setVerticalGroup(
			gl_messageBox.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_messageBox.createSequentialGroup()
					.addContainerGap()
					.addComponent(messageViewPanel, GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(messageSendPanel, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		
		JTextPane messageViewTextPanel = new JTextPane();
		messageViewTextPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		messageViewTextPanel.setEditable(false);
		messageViewPanel.setViewportView(messageViewTextPanel);
		
		// Define document
		doc = messageViewTextPanel.getStyledDocument();
		
		messageTextField = new JTextField();
		messageTextField.addActionListener(new SendMsgListener());
		//messageTextField.setColumns(10);
		
		JButton messageSendButton = new JButton("Send");
		messageSendButton.addActionListener(new SendMsgListener());
		
		GroupLayout gl_messageSendPanel = new GroupLayout(messageSendPanel);
		gl_messageSendPanel.setHorizontalGroup(
			gl_messageSendPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_messageSendPanel.createSequentialGroup()
					.addComponent(messageTextField, GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(messageSendButton, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE))
		);
		gl_messageSendPanel.setVerticalGroup(
			gl_messageSendPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_messageSendPanel.createParallelGroup(Alignment.BASELINE)
					.addComponent(messageTextField, GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
					.addComponent(messageSendButton, GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE))
		);
		messageSendPanel.setLayout(gl_messageSendPanel);
		messageBox.setLayout(gl_messageBox);
		
		JSplitPane userRoomPanel = new JSplitPane();
		userRoomPanel.setPreferredSize(new Dimension(250, 25));
		userRoomPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
		userRoomPanel.setResizeWeight(0.5);
		chatMain.setRightComponent(userRoomPanel);
		
		JScrollPane userRoomInfoPanel = new JScrollPane();
		userRoomPanel.setRightComponent(userRoomInfoPanel);
		
		JTextPane userRoomInfo = new JTextPane();
		userRoomInfo.setEditable(false);
		userRoomInfoPanel.setViewportView(userRoomInfo);
		
		JScrollPane userRoomTreePanel = new JScrollPane();
		userRoomPanel.setLeftComponent(userRoomTreePanel);
		
		treeModel = new DefaultTreeModel(clientObject.foyer);
		userRoomTree = new JTree(treeModel);
		userRoomTree.setMinimumSize(new Dimension(0, 200));
		userRoomTree.setDragEnabled(true);
		userRoomTree.setEditable(true);
		userRoomTreePanel.setViewportView(userRoomTree);
		
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(userRoomTree, popupMenu,this);
//		addPopup(userRoomTree, popupMenu);
		
		mnRoom = new JMenu("Room");
		mnRoom.setEnabled(false);
		popupMenu.add(mnRoom);
		
		JMenuItem mntmMoveHere = new JMenuItem("move here");
		mnRoom.add(mntmMoveHere);
		
		JMenuItem mntmCreateRoomHere = new JMenuItem("create room here");
		mntmCreateRoomHere.addActionListener(new CreateRoom());
		mnRoom.add(mntmCreateRoomHere);
		
		JMenuItem mntmRemoveRoom = new JMenuItem("remove room");
		mntmRemoveRoom.addActionListener(new DeleteRoom() );
		mnRoom.add(mntmRemoveRoom);
		
		JMenuItem mntmRenameRoom = new JMenuItem("rename room");
		mntmRenameRoom.addActionListener(new RenameRoom() );
		mnRoom.add(mntmRenameRoom);
		
		JMenu mnPrivate = new JMenu("private");
		mnRoom.add(mnPrivate);
		
		JMenuItem mntmCreatePrivateRoom = new JMenuItem("create private room here");
		mntmCreatePrivateRoom.addActionListener(new CreatePrivateRoom());
		mnPrivate.add(mntmCreatePrivateRoom);
		
		mntmMakeRoomPrivate = new JMenuItem("make room private");
		mntmMakeRoomPrivate.addActionListener(new MakePrivate());
		mnPrivate.add(mntmMakeRoomPrivate);
		
		mntmMakeRoomPublic = new JMenuItem("make room public");
		mntmMakeRoomPublic.addActionListener(new MakePublic());
		mnPrivate.add(mntmMakeRoomPublic);
		
		mnWhitelist = new JMenu("Whitelist");
		mnPrivate.add(mnWhitelist);
		
		JMenuItem mntmShowWhitelist = new JMenuItem("show whitelist");
		mntmShowWhitelist.addActionListener(new ShowWhitelist() );
		mnWhitelist.add(mntmShowWhitelist);
		
		JMenuItem mntmAddUserTo = new JMenuItem("add user to whitelist");
		mntmAddUserTo.addActionListener(new AddToWhitelist());
		mnWhitelist.add(mntmAddUserTo);
		
		JMenuItem mntmRemoveUserFrom = new JMenuItem("remove user from whitelist");
		mntmRemoveUserFrom.addActionListener(new RemoveFromWhitelist());
		mnWhitelist.add(mntmRemoveUserFrom);
		
		mnBlacklist = new JMenu("Blacklist");
		mnPrivate.add(mnBlacklist);
		
		JMenuItem mntmShowBlacklist = new JMenuItem("show blacklist");
		mntmShowBlacklist.addActionListener(new ShowBlacklist());
		mnBlacklist.add(mntmShowBlacklist);
		
		JMenuItem mntmAddUser = new JMenuItem("add user to blacklist");
		mntmAddUser.addActionListener(new AddToBlacklist());
		mnBlacklist.add(mntmAddUser);
		
		JMenuItem mntmRemoveUserFrom_1 = new JMenuItem("remove user from blacklist");
		mntmRemoveUserFrom_1.addActionListener(new RemoveFromBlacklist());
		mnBlacklist.add(mntmRemoveUserFrom_1);
		
		mntmMakeRoomPublic.addActionListener(new MakePublic() );
		mntmMakeRoomPrivate.addActionListener(new MakePrivate() );
		mntmCreatePrivateRoom.addActionListener(new CreatePrivateRoom() );
		mntmMoveHere.addActionListener(new MoveUser());
		
		mnUser = new JMenu("User");
		mnUser.setEnabled(false);
		popupMenu.add(mnUser);
		
		JMenuItem mntmBannUser = new JMenuItem("Bann User");
		mntmBannUser.addActionListener(new BannUser());
		mnUser.add(mntmBannUser);
		
		JMenuItem mntmUnbannUser = new JMenuItem("Unbann User");
		mntmUnbannUser.addActionListener(new UnBannUser());
		mnUser.add(mntmUnbannUser);
		
		JMenuItem mntmKickUser = new JMenuItem("Kick User");
		mntmKickUser.addActionListener(new KickUser()) ;
		mnUser.add(mntmKickUser);
		
		JMenuItem mntmWarnUser = new JMenuItem("Warn User");
		mntmWarnUser.addActionListener(new WarnUser());
		mnUser.add(mntmWarnUser);
		
		JMenuItem mntmWhisper = new JMenuItem("Whisper");
		mntmWhisper.addActionListener(new Whisper());
		mnUser.add(mntmWhisper);
	}
	
	class ClientConnectListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			clientObject.worker.connect(clientObject.prefs.get("IPaddress", "localhost"), clientObject.prefs.getInt("port", 3141));
		}
	}
	
	class DeleteHistory implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			clientObject.msgArray.clear();
			clientObject.worker.saveHistory();
			try {
				doc.remove(0, doc.getLength());
			} catch (BadLocationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	class ClientLoginListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			clientObject.worker.login(clientObject.prefs.get("username", "user"), clientObject.prefs.get("password", "test"));
			userRoomTree.updateUI();
		}
	}
	
	class ClientRegisterListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String username = (String) JOptionPane.showInputDialog(null, "Type a new username.", "Create New Room", JOptionPane.QUESTION_MESSAGE);
			String password = (String) JOptionPane.showInputDialog(null, "Type your new password.", "Create New Room", JOptionPane.QUESTION_MESSAGE);
			clientObject.worker.register(username,password);
			userRoomTree.updateUI();
		}
	}
	
	class ClientLogoutListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			clientObject.worker.logout();
		}
	}
	
	class ClientDisconnectListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			clientObject.worker.disconnect();
		}
	}
	
	class ClientExitListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			frmClient.dispose();
			
			clientObject.killClient();
		}
	}
	
	class OpenSettingsListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				SettingsWindow dialog = new SettingsWindow(clientObject);
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	class SendMsgListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(clientObject.isconnected) {
				String msgString = messageTextField.getText();
				
				if (!msgString.equals("")) {
					
					if (msgString.startsWith("$")) {									//checks if input is a command
						clientObject.worker.commander(msgString);				//yes? call commander
						messageTextField.setText("");
					} else if (clientObject.isconnected){
						clientObject.connectionThread.sendTextMessage(msgString);		//no? must be a message so send it to the server (if already connected)
						clientObject.UI.postOwnTextMessage(msgString);
						
						messageTextField.setText("");

					}else
						clientObject.worker.showStatus();	
					messageTextField.setText("");
//					clientObject.connectionThread.sendTextMessage(msgString);
//					clientObject.UI.postOwnTextMessage(msgString);
//					
//					messageTextField.setText("");
				}
			}
		}
	}
	
	public void postMessage(String msgReceived, String userName, String msgString) { 
		try {
			String temp="["+userName.toUpperCase()+" ("+msgReceived+")]"+">> "+msgString+"\n";
			doc.insertString(doc.getLength(), temp, null);
			clientObject.msgArray.add(temp);
			clientObject.worker.saveHistory();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
	
	public void postOwnMessage(String msgReceived, String userName, String msgString) {
		try {
			String temp="["+userName.toUpperCase()+" ("+msgReceived+")]"+">> "+msgString+"\n";
			doc.insertString(doc.getLength(), temp, null);
			clientObject.msgArray.add(temp);
			clientObject.worker.saveHistory();
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
	
	public void postText(String str) { 
		try {
			doc.insertString(doc.getLength(), str, null);
			
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}
	
	private static void addPopup(final Component component, final JPopupMenu popup, final MainWindow mainWindow) {
//		private static void addPopup(final Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					if(!popup.isShowing()){
					showMenu(e);
					}
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					if(!popup.isShowing()){
					showMenu(e);
					}
				}
			}
			private void showMenu(MouseEvent e) {
				if(component instanceof JTree){
					if(((JTree)component).getLastSelectedPathComponent() instanceof Room){
						
						mainWindow.mnRoom.setEnabled(true);
						mainWindow.mnUser.setEnabled(false);
						Room temp=(Room)((JTree)component).getLastSelectedPathComponent();
						if(temp.getOwner()!=null){
							mainWindow.mntmMakeRoomPrivate.setEnabled(false);
							mainWindow.mntmMakeRoomPublic.setEnabled(true);
							mainWindow.mnWhitelist.setEnabled(true);
							mainWindow.mnBlacklist.setEnabled(false);
						}else{
							mainWindow.mntmMakeRoomPrivate.setEnabled(true);
							mainWindow.mntmMakeRoomPublic.setEnabled(false);
							mainWindow.mnWhitelist.setEnabled(false);
							mainWindow.mnBlacklist.setEnabled(true);
						}
					}else if(((JTree)component).getLastSelectedPathComponent() instanceof User){
						mainWindow.mnRoom.setEnabled(false);
						mainWindow.mnUser.setEnabled(true);
					}else{
						mainWindow.mnRoom.setEnabled(false);
						mainWindow.mnUser.setEnabled(false);
					}
				}else{
					mainWindow.mnRoom.setEnabled(false);
					mainWindow.mnUser.setEnabled(false);
				}
				
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	
	class MoveUser implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room)  userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = new ArrayList<String>();
			path.clear();
			for(int i=0;i<node.getDir().size();i++){
			path.add(node.getDir().get(i));
			}
			clientObject.worker.changeRoom(path);
		}
	}
	
	class BannUser implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			User node=(User) userRoomTree.getLastSelectedPathComponent();
			clientObject.connectionThread.sendSysMessage(CATEGORY.MODERATE,MESSAGE.BANN_USER,node.name);
		}
	}
	class UnBannUser implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			
			String user=JOptionPane.showInputDialog(frmClient, "please type the name of the user", "Unbann",  JOptionPane.QUESTION_MESSAGE);
			clientObject.connectionThread.sendSysMessage(CATEGORY.MODERATE,MESSAGE.UNBANN_USER,user);
		}
	}
	
	class KickUser implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			User node=(User) userRoomTree.getLastSelectedPathComponent();
			clientObject.connectionThread.sendSysMessage(CATEGORY.MODERATE,MESSAGE.KICK_USER,node.name);
		}
	}
	class WarnUser implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			User node=(User) userRoomTree.getLastSelectedPathComponent();
			String reason=JOptionPane.showInputDialog(frmClient, "please type a reason for the warning", "Reason",  JOptionPane.QUESTION_MESSAGE);
			clientObject.worker.warnUser(node.name, reason);
		}
	}
	class Whisper implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			User node = (User) userRoomTree.getLastSelectedPathComponent();
			String message=(String) JOptionPane.showInputDialog(frmClient, "type in your message", "whispering", JOptionPane.QUESTION_MESSAGE);
			clientObject.worker.whisper(node.name, message);
		}
	}
	
	class CreateRoom implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			Room node=(Room) userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = new ArrayList<String>();
			path.clear();
			for(int i=0;i<node.getDir().size();i++){
			path.add(node.getDir().get(i));
			}
			String newRoom = (String) JOptionPane.showInputDialog(frmClient, "Type a name for the new room.", "Create New Room", JOptionPane.QUESTION_MESSAGE);
			if (newRoom!=null && !newRoom.isEmpty()) {
				path.add(newRoom);
				clientObject.worker.createRoom(path);
			}
			
		}
	}
	
	class CreatePrivateRoom implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			Room node=(Room) userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = new ArrayList<String>();
			path.clear();
			for(int i=0;i<node.getDir().size();i++){
			path.add(node.getDir().get(i));
			}
			String newRoom = (String) JOptionPane.showInputDialog(frmClient, "Type a name for the new room.", "Create New Room", JOptionPane.QUESTION_MESSAGE);
			if (newRoom!=null && !newRoom.isEmpty()) {
				path.add(newRoom);
				clientObject.worker.createPrivateRoom(path);
			}
			
		}
	}
	
	class RenameRoom implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room)  userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = new ArrayList<String>();
			path.clear();
			for(int i=0;i<node.getDir().size();i++){
			path.add(node.getDir().get(i));
			}
			String newName = (String) JOptionPane.showInputDialog(frmClient, "Type the new name for the room \"" + path.get(path.size()-1) + "\".", "Create New Room", JOptionPane.QUESTION_MESSAGE);
			if (newName!=null && !newName.isEmpty()) {
				path.add(newName);
				clientObject.worker.renameRoom(path, newName);
			}
		}
	}
	
	class DeleteRoom implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room) userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = node.getDir();
			
			if (JOptionPane.showConfirmDialog(frmClient, "Do you really want to remove the room?", "Remove Room", JOptionPane.YES_NO_OPTION) == 0) {			
				clientObject.worker.deleteRoom(path);
			}
		}
	}
	
	class MakePrivate implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room) userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = node.getDir();
			
			if (JOptionPane.showConfirmDialog(frmClient, "Do you want to make this room private?", "Make private", JOptionPane.YES_NO_OPTION) == 0) {			
				clientObject.worker.makeRoomPrivate(path);
			}
		}
	}
	
	class MakePublic implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room) userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = node.getDir();
			
			if (JOptionPane.showConfirmDialog(frmClient, "Do you want to make this room public?", "Make public", JOptionPane.YES_NO_OPTION) == 0) {			
				clientObject.worker.makeRoomPublic(path);
			}
		}
	}
	
	class AddToWhitelist implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room)  userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = new ArrayList<String>();
			path.clear();
			for(int i=0;i<node.getDir().size();i++){
			path.add(node.getDir().get(i));
			}
			String name = (String) JOptionPane.showInputDialog(frmClient, "Enter a username!", "Add to whitelist", JOptionPane.QUESTION_MESSAGE);
			if (name!=null && !name.isEmpty()) {
				clientObject.worker.addUserToWhitelist(path, name);
			}
		}
	}
	
	class RemoveFromWhitelist implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room)  userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = new ArrayList<String>();
			path.clear();
			
			for(int i=0;i<node.getDir().size();i++){
			path.add(node.getDir().get(i));
			}
			int i=JOptionPane.showOptionDialog(frmClient, "Choose a user to be removed from the whitelist!", "Remove user", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, node.getWhitelist(), null);
			if(i!=-1){
				clientObject.worker.removeUserFromWhitelist(path, node.getWhitelist()[i]);
			}
			
		}
	}
	
	class AddToBlacklist implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room)  userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = new ArrayList<String>();
			path.clear();
			for(int i=0;i<node.getDir().size();i++){
			path.add(node.getDir().get(i));
			}
			String name = (String) JOptionPane.showInputDialog(frmClient, "Enter a username!", "Add to blacklist", JOptionPane.QUESTION_MESSAGE);
			if (name!=null && !name.isEmpty()) {
				clientObject.worker.addUserToBlacklist(path, name);
			}
		}
	}
	
	class RemoveFromBlacklist implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room)  userRoomTree.getLastSelectedPathComponent();
			java.util.List<String> path = new ArrayList<String>();
			path.clear();
			
			for(int i=0;i<node.getDir().size();i++){
			path.add(node.getDir().get(i));
			}
			int i=JOptionPane.showOptionDialog(frmClient, "Choose a user to be removed from the blacklist!", "Remove user", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, node.getBlacklist(), null);
			if(i!=-1){
				clientObject.worker.removeUserFromBlacklist(path, node.getBlacklist()[i]);
			}
			
		}
	}
	
	class ShowWhitelist implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room)  userRoomTree.getLastSelectedPathComponent();
			
			String separator = System.getProperty("line.Separator");
			JOptionPane.showMessageDialog(frmClient, node.getWhitelist(), "Whitelist", JOptionPane.INFORMATION_MESSAGE, null);
			
		}
	}
	
	class ShowBlacklist implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			Room node = (Room)  userRoomTree.getLastSelectedPathComponent();
			
			String separator = System.getProperty("line.Separator");
			JOptionPane.showMessageDialog(frmClient, node.getBlacklist(), "Blacklist", JOptionPane.INFORMATION_MESSAGE, null);
			
		}
	}
	
	
	public void want_on_whitelist(){
		String temp="";
		for(int i=0;i<clientObject.worker.want_on_whitelist_dir.size();i++){
			temp=temp+clientObject.worker.want_on_whitelist_dir.get(i)+"/";
		}
		if(JOptionPane.showConfirmDialog(frmClient, "Do you want to allow user "+clientObject.worker.want_on_whitelist_name+" to enter room "+temp, "Allow User", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==0){
			clientObject.worker.want_on_whitelist_accept();
		}else{
			clientObject.worker.want_on_whitelist_denied();
		}
	}
}
